/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.Initializer;

import com.j4dream.property.dao.GeoDao;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Geographic {

    public static List<GeoDto> getAllGeos() {
        List<GeoDto> list = new ArrayList<>();
        try {
            FileReader fr = new FileReader("src/main/resources/geographic.json");
            BufferedReader bf = new BufferedReader(fr);
            String jsonStr = "";
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = bf.readLine())!=null) {
                sb.append(line);
            }
            bf.close();
            jsonStr = sb.toString();
            JSONTokener jt = new JSONTokener(jsonStr);
            JSONObject  jo = new JSONObject(jt);
            Iterator<String> it = jo.keys();
            Iterator<String> subIt = null;
            while (it.hasNext()) {
                String nx = it.next();
                JSONObject co = (JSONObject) jo.get(nx);
                subIt = co.keys();
                while (subIt.hasNext()) {
                    String snx = subIt.next();
                   list.add(new GeoDto(snx, co.get(snx).toString(), nx));
                }
            }
        } catch (Exception e) {

        }

        return list;
    }
}
